package com.hejia.bulbul.controller;

import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import com.hejia.alauda.entity.MediaInstitution;
import com.hejia.alauda.entity.Medium;
import com.hejia.alauda.service.PageService;
import com.hejia.alauda.utils.Pager;

/**
 * 
 * @author jatin
 *
 */
@Controller
@RequestMapping("medium")
public class MediumController extends BaseController {

	@Resource
	private PageService mediumService;

	@Resource
	private PageService mediaInstitutionService;

	@RequestMapping(value = "mediumlist", method = RequestMethod.GET)
	public String mediumList(ModelMap model, Pager<Medium> pager) {
		Pager<Medium> mediums = mediumService.findAllMedium(pager);
		model.put("mediums", mediums);
		return "/page/medium_list";
	}

	/**
	 * 跳转到添加界面
	 */
	@RequestMapping(value = "mediumadd", method = RequestMethod.GET)
	public String addMedium(ModelMap model) {
		List<MediaInstitution> miList = mediaInstitutionService
				.getMediumnameList();
		model.put("miList", miList);
		return "/page/medium_input";
	}

	/**
	 * 添加完保存
	 */
	@RequestMapping(value = "mediumadd", method = RequestMethod.POST)
	public String saveMedium(Medium medium, ModelMap model, Long institutionId,
			RedirectAttributes redirectAttributes) {
		MediaInstitution institution = mediaInstitutionService
				.getMediaInstitutionById(institutionId);
		medium.setInstitution(institution);
		if (mediumService.saveMedium(medium)) {
			redirectAttributes.addFlashAttribute("status", STATUS_SUCCESS);
			redirectAttributes.addFlashAttribute("msg", "媒体报道添加成功");
		} else {
			redirectAttributes.addFlashAttribute("status", STATUS_FAILD);
			redirectAttributes.addFlashAttribute("msg", "媒体报道添加失败");
		}

		return "redirect:/medium/mediumlist";
	}

	/**
	 * 跳转到修改界面
	 */
	@RequestMapping(value = "mediumupdate/{id}", method = RequestMethod.GET)
	public String updateMedium(@PathVariable Long id, ModelMap model) {
		Medium medium = mediumService.getMediumById(id);
		model.put("medium", medium);
		List<MediaInstitution> miList = mediaInstitutionService
				.getMediumnameList();
		model.put("miList", miList);

		return "/page/medium_input";
	}

	/**
	 * 修改完保存
	 */
	@RequestMapping(value = "mediumupdate/{id}", method = RequestMethod.POST)
	public String saveUpdateMedium(@PathVariable Long id, Medium medium,
			Long institutionId, RedirectAttributes redirectAttributes) {
		MediaInstitution institution = mediaInstitutionService
				.getMediaInstitutionById(institutionId);
		medium.setInstitution(institution);
		if (mediumService.modifyMedium(medium)) {
			redirectAttributes.addFlashAttribute("status", STATUS_SUCCESS);
			redirectAttributes.addFlashAttribute("msg", "媒体报道修改成功");
		} else {
			redirectAttributes.addFlashAttribute("status", STATUS_FAILD);
			redirectAttributes.addFlashAttribute("msg", "媒体报道修改失败");
		}

		return "redirect:/medium/mediumlist";
	}

	/**
	 * 删除
	 */
	@RequestMapping(value = "mediumdelete/{id}", method = RequestMethod.GET)
	public String deleteMedium(@PathVariable Long id,
			RedirectAttributes redirectAttributes) {
		if (mediumService.deleteMedium(id)) {
			redirectAttributes.addFlashAttribute("status", STATUS_SUCCESS);
			redirectAttributes.addFlashAttribute("msg", "媒体报道删除成功");
		} else {
			redirectAttributes.addFlashAttribute("status", STATUS_FAILD);
			redirectAttributes.addFlashAttribute("msg", "媒体报道删除失败");
		}

		return "redirect:/medium/mediumlist";
	}

}
